{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "16e7f562",
   "metadata": {},
   "source": [
    "# week07课堂练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "460d96a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.d7ef34148293bd9bc59fc7f3a20fb2b1.2592000.1652325640.282335-25948884'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'cGoNLhYSUWmePib17caILEmv',\n",
    "    'client_secret':'jOqbRG6cxLzb2RK2XrNBb3b7vuGYWacQ'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "token = response.json()['access_token']\n",
    "token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c8b2d1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装函数\n",
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path,url):\n",
    "    request_url = url\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"id_card_side\":\"front\"\n",
    "             }\n",
    "    access_token = token\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(request_url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "371e9ecf",
   "metadata": {},
   "source": [
    "### 通用场景文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b718ddb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '魍魉魑魅'}, {'words': '饕餮辟邪獬豸貔貅'}],\n",
       " 'words_result_num': 2,\n",
       " 'log_id': 1513718956549753256}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通用文字识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic\"\n",
    "everything(token,\"general_basic.jpg\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67dcb09a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '梦想起航'},\n",
       "  {'words': '前往下一个目的地'},\n",
       "  {'words': '开始新的旅程'}],\n",
       " 'words_result_num': 3,\n",
       " 'log_id': 1513719060281905341}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 网络图片文字识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "everything(token,\"webimage.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "44e52325",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'location': {'top': 224,\n",
       "    'left': 287,\n",
       "    'width': 520,\n",
       "    'height': 87},\n",
       "   'words': '《演讲的力量》'},\n",
       "  {'location': {'top': 318, 'left': 166, 'width': 1369, 'height': 98},\n",
       "   'words': '任何一个人,只要拥有值得分享的思想,就能'},\n",
       "  {'location': {'top': 417, 'left': 86, 'width': 1433, 'height': 101},\n",
       "   'words': '发表精彩的演讲,在公共演讲中,唯一真正重要的'},\n",
       "  {'location': {'top': 515, 'left': 90, 'width': 1464, 'height': 99},\n",
       "   'words': '东西不是自信,不是舞台展示,也不是流利的语言,'},\n",
       "  {'location': {'top': 627, 'left': 107, 'width': 635, 'height': 86},\n",
       "   'words': '而是有价值的思想'},\n",
       "  {'location': {'top': 705, 'left': 938, 'width': 542, 'height': 105},\n",
       "   'words': '一克里斯安德森'}],\n",
       " 'words_result_num': 6,\n",
       " 'log_id': 1513719630876210682}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 手写文字识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "everything(token,\"handwriting.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "18a799c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'codes_result': [{'text': ['http://qr61.cn/oby3jZ/q1APkQu'],\n",
       "   'type': 'QR_CODE'}],\n",
       " 'codes_result_num': 1,\n",
       " 'log_id': 1513720393882398786}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维码识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "everything(token,\"code.png\",url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8211a7c3",
   "metadata": {},
   "source": [
    "### 卡片文本识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4b307b48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': {'姓名': {'location': {'top': 438,\n",
       "    'left': 648,\n",
       "    'width': 280,\n",
       "    'height': 118},\n",
       "   'words': '王飞'},\n",
       "  '民族': {'location': {'top': 656, 'left': 1156, 'width': 90, 'height': 104},\n",
       "   'words': '汉'},\n",
       "  '住址': {'location': {'top': 1058, 'left': 640, 'width': 921, 'height': 111},\n",
       "   'words': '辽宁省大连市甘井子区'},\n",
       "  '公民身份号码': {'location': {'top': 1574,\n",
       "    'left': 1042,\n",
       "    'width': 1393,\n",
       "    'height': 127},\n",
       "   'words': '522530199208180048'},\n",
       "  '出生': {'location': {'top': 849, 'left': 621, 'width': 855, 'height': 103},\n",
       "   'words': '19920818'},\n",
       "  '性别': {'location': {'top': 654, 'left': 653, 'width': 94, 'height': 108},\n",
       "   'words': '女'}},\n",
       " 'words_result_num': 6,\n",
       " 'idcard_number_type': 1,\n",
       " 'image_status': 'normal',\n",
       " 'log_id': 1513721444559549781}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 身份证识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "everything(token,\"idcard.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1b918e74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': {'valid_date': '07/22',\n",
       "  'bank_card_number': '6225 9700 7000 3000',\n",
       "  'bank_name': '中国工商银行',\n",
       "  'bank_card_type': 2,\n",
       "  'holder_name': 'MR.CHENTA'},\n",
       " 'direction': 0,\n",
       " 'log_id': 1513721941361931592}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 银行卡识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard\"\n",
    "everything(token,\"bankcard.png\",url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "668bbf4f",
   "metadata": {},
   "source": [
    "### 财务票据文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7a007988",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'log_id': 1513723137782063099,\n",
       " 'error_msg': 'image size error',\n",
       " 'error_code': 216202}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 增值税发票识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "everything(token,\"vat_invoice.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "175c9af7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result_num': 13,\n",
       " 'words_result': {'id_num': '2302051998****156X',\n",
       "  'serial_number': '10010301110403F067846',\n",
       "  'ticket_num': 'F067846',\n",
       "  'seat_num': '02车03C号',\n",
       "  'time': '09:36',\n",
       "  'date': '2019年04月03日',\n",
       "  'name': '裴瑜丽',\n",
       "  'destination_station': '天津站',\n",
       "  'seat_category': '二等座',\n",
       "  'starting_station': '北京南站',\n",
       "  'sales_station': '北京南',\n",
       "  'train_num': 'C2565',\n",
       "  'ticket_rates': '￥54.5元'},\n",
       " 'direction': 0,\n",
       " 'log_id': 1513723146424231906}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 火车票识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/train_ticket\"\n",
    "everything(token,\"train_ticket.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10ee507b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
